//
//  GoodContentCell.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/25.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "GoodContentCell.h"

#import <WebKit/WebKit.h>

@interface GoodContentCell()

@property (nonatomic,strong) UITextView *contentTextView;

@property (nonatomic,strong) UIView *leftFlagView;

@property (nonatomic,strong) UILabel *leftFlagLB;

@end

@implementation GoodContentCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}


#pragma mark - setter
- (void)setGoodContent:(NSString *)goodContent{
    _goodContent = goodContent;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //对图片大小进行处理，适应屏幕宽度
        NSString *newString = [_goodContent stringByReplacingOccurrencesOfString:@"<img" withString:[NSString stringWithFormat:@"<img width=\"%f\"",ScreenWidth - 10]];
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[newString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        self.contentTextView.attributedText = attributedString;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.contentTextView.height = self.contentTextView.contentSize.height;
            if (self.updateCellHeightBlock) {
                self.updateCellHeightBlock(self.contentTextView.height);
            }
        });
    });
}

#pragma mark - UI
-(void)initUI{
    
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 12)];
    lineV.backgroundColor = MainBackColor;
    [self.contentView addSubview:lineV];
    [self.contentView addSubview:self.leftFlagView];
    [self.contentView addSubview:self.leftFlagLB];
    [self.contentView addSubview:self.contentTextView];
}
#pragma mark - lazy
- (UIView *)leftFlagView{
    if (!_leftFlagView) {
        _leftFlagView = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 4, 14)];
        _leftFlagView.backgroundColor = RGB(26, 128, 255);
        _leftFlagView.clipsToBounds = YES;
        _leftFlagView.layer.cornerRadius = 2;
    }
    return _leftFlagView;
}

- (UILabel *)leftFlagLB{
    if (!_leftFlagLB) {
        _leftFlagLB = [[UILabel alloc] initWithFrame:CGRectMake(self.leftFlagView.right+8, self.leftFlagView.top-3, ScreenWidth/2+20, 20)];
        _leftFlagLB.textColor = MainBlackTextColor;
        _leftFlagLB.font = FontSet(14);
        _leftFlagLB.text = @"商品详情";
    }
    return _leftFlagLB;
}
- (UITextView *)contentTextView{
    if (!_contentTextView) {
        _contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, self.leftFlagLB.bottom+15, ScreenWidth, 200)];
        _contentTextView.userInteractionEnabled = NO;
    }
    return _contentTextView;
}



//html 编码
- (NSString *)htmlEntityDecode:(NSString*)string{
    string = [string stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    string = [string stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    return string;
}



@end
